package com.wbao.dianniu.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.wbao.dianniu.R;
import com.wbao.dianniu.adapter.FriendListAdapter;
import com.wbao.dianniu.data.HeadData;
import com.wbao.dianniu.listener.IFansListListener;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.FansListManager;
import com.wbao.dianniu.utils.ActivityUtils;

import java.util.List;

/**
 * 粉丝
 *
 * @author yejianjun
 */
public class FansListActivity extends BaseActivity implements IFansListListener, FriendListAdapter.OnItemClickListener {
    private LinearLayout waittingLayout;
    private ProgressBar waittingProBar;
    private TextView waittingTV;
    private FriendListAdapter adapter;
    private PullToRefreshListView pullListView;
    private FansListManager manager;
    private int friendId = 0;
    private int ROWS = 10;
    private TextView noDataTV;
    //是否是下拉刷新
    private boolean isPullDown = false;
    private ListView myListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_follow_list, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        setTitleName(getResources().getString(R.string.fans));

        friendId = getIntent().getIntExtra(Const.INTENT_ID, GlobalContext.getAccountId());
        initWattingView();
        initManager();
        initView();
        reqPullUpData();
    }

    private void initWattingView() {
        waittingLayout = findViewById(R.id.waitting_layout);
        waittingProBar = findViewById(R.id.waitting_progress);
        waittingTV = findViewById(R.id.waitting_text_desc);
    }

    private void initView() {
        noDataTV = findViewById(R.id.no_find_data);

        pullListView = findViewById(R.id.follow_list_recycler);
        pullListView.setMode(PullToRefreshListView.Mode.BOTH);
        pullListView.setOnRefreshListener(refreshListener2);
        myListView = pullListView.getRefreshableView();
        registerForContextMenu(myListView);
        adapter = new FriendListAdapter(this);
        adapter.setItemClickListener(this);
        myListView.setAdapter(adapter);

    }

    PullToRefreshBase.OnRefreshListener2 refreshListener2 = new PullToRefreshBase.OnRefreshListener2() {
        @Override
        public void onPullDownToRefresh() {
            reqPullDownData();
        }

        @Override
        public void onPullUpToRefresh() {
            reqPullUpData();
        }
    };

    private void initManager() {
        manager = new FansListManager(this);
        manager.addFansListListener(this);
    }

    private void reqPullDownData() {
        isPullDown = true;
        manager.fansList(friendId, 0, ROWS);
    }

    private void reqPullUpData() {
        isPullDown = false;
        manager.fansList(friendId, adapter.getList().size(), ROWS);
    }

    @Override
    public void onFansListFailure(int errorCode, String errorMsg) {
        waittingLayout.setVisibility(View.VISIBLE);
        waittingProBar.setVisibility(View.GONE);
        waittingTV.setText(errorMsg == null ? "" : errorMsg);
        pullListView.onRefreshComplete(false, false);
    }

    @Override
    public void onFansListSuccess(List<HeadData> list) {
        waittingLayout.setVisibility(View.GONE);
        pullListView.onRefreshComplete(false, false);
        if (isPullDown) {
            adapter.setData(list);
        } else {
            adapter.addData(list);
        }
        if (adapter.getList().size() == 0) {
            noDataTV.setVisibility(View.VISIBLE);
        } else {
            noDataTV.setVisibility(View.GONE);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != manager) {
            manager.removeFansListListener(this);
        }

    }

    @Override
    public void itemClick(View view) {
        int position;
        switch (view.getId()) {
            case R.id.friend_list_layout:
                position = (Integer) view.getTag(R.id.friend_list_layout);
                ActivityUtils.intoHeadInfoActivity(adapter.getList().get(position).accountId, FansListActivity.this);
                break;

            default:
                break;
        }
    }

}
